package com.bl.student.api.controller.task;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.service.biz.apply.ApplyService;
import com.bl.student.service.biz.task.TaskService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.comment.CommentAddVo;
import com.bl.student.service.vo.confirm.ConfirmAddVo;
import com.bl.student.service.vo.confirm.ConfirmListVo;
import com.bl.student.service.vo.confirm.ConfirmRefundVo;
import com.bl.student.service.vo.task.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liangweikang on 18/1/29.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis/task"})
@Api(value = "发布任务 ", description = "发布任务相关api")
public class TaskController extends BaseController {
    @Autowired
    TaskService taskService;

    @ApiOperation(value = "根据甲方id，status获取发布列表 PRE 发布 ONLINE 正在进行 FINISH 已完成 FAIL 失败 ALL 所有", notes = "发布记录 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "任务状态 PRE 发布 ONLINE 正在进行 FINISH 已完成 FAIL 失败 ALL 所有", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, paramType = "query", dataType = "int", defaultValue = "10")})

    @GetMapping(value = {"/get_task_list_partya"})
    public ApiResponseBody<TaskListVo> getListsPartya(
            @RequestParam(value = "status", required = true) String status,
            @RequestParam(value = "page", required = true, defaultValue = "1") int page,
            @RequestParam(value = "size", required = true, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);
        TaskListVo taskListVo = taskService.getTaskListsPartya(getUserInfo(request).getId(), status, pageable);

        return ApiResponseBody.createSuccessBody(taskListVo);
    }

    @ApiOperation(value = "根据乙方id，status获取发布列表 PRE 发布 ONLINE 正在进行 BFINSH 乙方确认完成 FINIISH 已完成 FAIL 失败 ALL 所有", notes = "发布记录 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "任务状态 PRE 发布 ONLINE 正在进行 FINISH 已完成 FAIL 失败 ALL 所有", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, paramType = "query", dataType = "int", defaultValue = "10")})

    @GetMapping(value = {"/get_task_list_partyb"})
    public ApiResponseBody<TaskListVo> getListsPartyb(
            @RequestParam(value = "status", required = true) String status,
            @RequestParam(value = "page", required = true, defaultValue = "1") int page,
            @RequestParam(value = "size", required = true, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);
        TaskListVo taskListVo = taskService.getTaskListsPartyb(getUserInfo(request).getId(), status, pageable);

        return ApiResponseBody.createSuccessBody(taskListVo);
    }

    @ApiOperation(value = "多条件筛选,排序类型 time-时间，distance-距离，grade-评分", notes = "多条件筛选 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/get_task_list_multi"})
    public ApiResponseBody modify(
            @Valid @RequestBody TaskSearchVo body, BindingResult result,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(body.getPage() - 1, body.getSize());

        TaskListVo taskListVo = taskService.getTaskListsMulti(body.getClassc(),body.getClassb(),body.getClassa(),body.getOnline(),body.getSex(),body.getPrice(),body.getLanguage(),body.getGrade(),body.getCollege(),body.getType(),
                body.getStar(),body.getLocation(),body.getTime(),body.getCount(),body.getRole(),body.getChinese(),body.getEnv(),body.getTSex(),pageable,
                body.getStatus(), body.getLongitude(), body.getLatitude(), body.getBuyType(), body.getRunType(), body.getSortType());

        return ApiResponseBody.createSuccessBody(taskListVo);
    }


    @ApiOperation(value = "新增一条任务 PRE状态", notes = "增加新发布的任务 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add"})
    public ApiResponseBody add(
            @Valid @RequestBody TaskAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        taskService.add(getUserInfo(request).getId(),body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改任务内容", notes = "修改任务 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody modify(
            @Valid @RequestBody TaskModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        taskService.modify(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "乙方确认完成 ONLINE -> BFINISH", notes = "乙方确认完成 ONLINE -> BFINISH")
    @ApiImplicitParams({})
    @PostMapping(value = {"/confirm"})
    public ApiResponseBody confirm(
            @Valid @RequestBody ConfirmAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        taskService.confirm(body, getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "拒绝", notes = "拒绝 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/refuse"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {

        taskService.refuse(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "退款", notes = "退款 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/refund"})
    public ApiResponseBody refund(
            @Valid @RequestBody ConfirmRefundVo body, BindingResult result,
            HttpServletRequest request) {

        taskService.refund(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "甲方确认完成 BFINSH -> FINISH", notes = "甲方确认完成 BFINSH -> FINISH")
    @ApiImplicitParams({})
    @PostMapping(value = {"/comment"})
    public ApiResponseBody comment(
            @Valid @RequestBody CommentAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        taskService.addComment(body, getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "甲方批量确认完成 BFINSH -> FINISH", notes = "甲方确认完成 BFINSH -> FINISH")
    @ApiImplicitParams({})
    @PostMapping(value = {"/comment_list"})
    public ApiResponseBody commentList(
            @Valid @RequestBody DelBody body,BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        taskService.addCommentList(body.getId(), getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "任务详情 非登录", notes = "发布记录 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "longitude", value = "longitude", required = true, paramType = "query", dataType = "double"),
            @ApiImplicitParam(name = "latitude", value = "latitude", required = true, paramType = "query", dataType = "double")})

    @GetMapping(value = {"/get_task_detail"})
    public ApiResponseBody<TaskDetailAVo> get(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "longitude", required = true) double longitude,
            @RequestParam(value = "latitude", required = true) double latitude,
            HttpServletRequest request) {
        TaskDetailAVo taskDetailVo = taskService.getTaskDetail(id, getUserInfo(request), longitude, latitude);

        return ApiResponseBody.createSuccessBody(taskDetailVo);
    }

    @ApiOperation(value = "任务详情 登录", notes = "发布记录 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", required = true, paramType = "query", dataType = "String")})

    @GetMapping(value = {"/get_detail_login"})
    public ApiResponseBody<TaskDetailAVo> getDetail(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "longitude", required = true) double longitude,
            @RequestParam(value = "latitude", required = true) double latitude,
            HttpServletRequest request) {
        TaskDetailAVo taskDetailVo = taskService.getTaskDetail(id, getUserInfo(request), longitude, latitude);

        return ApiResponseBody.createSuccessBody(taskDetailVo);
    }

    @ApiOperation(value = "验收记录", notes = "验收记录 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, paramType = "query", dataType = "String")})

    @GetMapping(value = {"/get_confirm_list"})
    public ApiResponseBody<ConfirmListVo> confirmList(
            @RequestParam(value = "taskId", required = true) String taskId,

            HttpServletRequest request) {
         ConfirmListVo confirmListVo = taskService.confirmList(taskId);

        return ApiResponseBody.createSuccessBody(confirmListVo);
    }





}
